package github.kawaiior.yuanchuxiuzhen.common.item.misc;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.herb.IHerbEffect;
import github.kawaiior.yuanchuxiuzhen.core.init.HerbEffectInit;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.init.itemgroup.YcxzGroup;
import github.kawaiior.yuanchuxiuzhen.util.MathUtil;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Random;

public class RefinePelletItem extends PelletItem{
    public RefinePelletItem(ManaPower.ManaRoot manaRoot) {
        super(manaRoot, new Properties().group(YcxzGroup.YCXZ_GROUP).maxStackSize(1));
    }

    /**
     * 异火等级越高
     * 返回的数组数值越大
     * @param fireLevel 异火等级
     * @return byte数组
     */
    private static byte[] getRandomByteArray(byte fireLevel, int size){
        byte[] randomArray = new byte[size];
        for (int i = 0; i < size; i++) {
            randomArray[i] = (byte) (MathUtil.RANDOM.nextInt(fireLevel) + 1);
        }
        return randomArray;
    }

    public static void initRefinePellet(ItemStack stack, byte fireLevel, byte[] effects){
        CompoundNBT nbt = stack.getOrCreateTag();

        nbt.putByteArray("refine_pellet_effect", effects);
        nbt.putByte("fire_level", fireLevel);
        nbt.putByteArray("random_levels", getRandomByteArray(fireLevel, effects.length));

//        stack.setTag(nbt);
    }

    /**
     * 根据五行获取对应的丹药
     */
    public static ItemStack getBaseRefinePellet(ManaPower.ManaRoot manaRoot){
        if (manaRoot == ManaPower.ManaRoot.METAL){
            return new ItemStack(ItemInit.REFINE_PELLET_ITEM_0.get());
        }
        else if (manaRoot == ManaPower.ManaRoot.WOOD){
            return new ItemStack(ItemInit.REFINE_PELLET_ITEM_1.get());
        }
        else if (manaRoot == ManaPower.ManaRoot.WATER){
            return new ItemStack(ItemInit.REFINE_PELLET_ITEM_2.get());
        }
        else if (manaRoot == ManaPower.ManaRoot.FIRE){
            return new ItemStack(ItemInit.REFINE_PELLET_ITEM_3.get());
        }
        else if (manaRoot == ManaPower.ManaRoot.EARTH){
            return new ItemStack(ItemInit.REFINE_PELLET_ITEM_4.get());
        }
        return ItemStack.EMPTY;
    }

    private void onUsed(PlayerEntity player, World world, ItemStack stack){
        CompoundNBT nbt = stack.getOrCreateTag();

        byte[] effects = nbt.getByteArray("refine_pellet_effect");
        byte fireLevel = nbt.getByte("fire_level");
        byte[] randomLevels = nbt.getByteArray("random_levels");
        if (effects.length == 0){
            Yuanchuxiuzhen.debug("丹药中没有写入信息");
            return;
        }
        if (fireLevel == 0){
            Yuanchuxiuzhen.debug("丹药中没有写入异火等级");
            return;
        }
        if (effects.length != randomLevels.length){
            Yuanchuxiuzhen.debug("效果或等级NBT数据异常");
            return;
        }
        for (byte i = 0; i < 4; i++) {
            IHerbEffect herbEffect = HerbEffectInit.herbEffectArray[effects[i]];
            herbEffect.onPlayerUse(player, fireLevel, randomLevels[i]);
        }
    }

    @Override
    public ItemStack onItemUseFinish(ItemStack stack, World world, LivingEntity entityLiving) {

        if (entityLiving instanceof PlayerEntity && !entityLiving.world.isRemote){
            onUsed((PlayerEntity) entityLiving, world, stack);
        }
        return super.onItemUseFinish(stack, world, entityLiving);
    }

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        super.addInformation(stack, worldIn, tooltip, flagIn);
        CompoundNBT nbt = stack.getOrCreateTag();
        byte[] effects = nbt.getByteArray("refine_pellet_effect");
        byte fireLevel = nbt.getByte("fire_level");
        byte[] randomLevels = nbt.getByteArray("random_levels");
        if (effects.length==randomLevels.length && fireLevel!=0 && effects.length!=0){
            tooltip.add(new StringTextComponent(""));
            tooltip.add(new TranslationTextComponent("pellet.effect.addon"));
            for (byte i = 0; i < effects.length; i++) {
                IHerbEffect herbEffect = HerbEffectInit.herbEffectArray[effects[i]];
                tooltip.add(herbEffect.getHerbEffectTextComponent(fireLevel, randomLevels[i]));
            }
        }
    }
}
